﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NationalFlagHead
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 切换五星红旗模板集合下标
        /// </summary>
        int index = 0;
        /// <summary>
        /// 五星红旗模板集合
        /// </summary>
       private static FileInfo[] templateFileInfos = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(Application.StartupPath + "\\Template");
            templateFileInfos = directoryInfo.GetFiles();
        }
        /// <summary>
        /// 选择头像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_select_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//初始路径为桌面
            openFileDialog.Filter = "头像图片|*.png;*.jpg";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                pic_old.Image = Image.FromFile(openFileDialog.FileName);
            }
        }
      
        /// <summary>
        /// 生成或切换模板事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_change_Click(object sender, EventArgs e)
        {
            GenerateOrChangeTemplate();
        }
        
        /// <summary>
        /// 生成头像或切换模板生成头像
        /// </summary>
        private void GenerateOrChangeTemplate()
        {
            try
            {
                if (templateFileInfos.Length == 0)
                {
                    MessageBox.Show("红旗模板集为空，请添加", "提示", MessageBoxButtons.OK);
                    return;
                }

                if (index >= templateFileInfos.Length)
                {
                    index = 0;
                }
                Image head = (Image)pic_old.Image.Clone();
                Image template = Image.FromFile(templateFileInfos[index].FullName);
                Graphics graphics = Graphics.FromImage(head);

                if (templateFileInfos[index].Name.StartsWith("all_"))
                {
                    graphics.DrawImage(template, 0, 0, head.Width, head.Height);
                }
                else if (templateFileInfos[index].Name.StartsWith("right_"))
                {
                    int x = head.Width / 4 * 3;
                    int y = head.Height / 4 * 3;
                    int w = head.Width / 4;
                    int h = head.Height / 4;
                    graphics.DrawImage(template, x, y, w, h);
                }
                else if (templateFileInfos[index].Name.StartsWith("left_"))
                {
                    int y = head.Height - template.Height;
                    if (y < 0) y = 0;
                    graphics.DrawImage(template, 0, y, head.Width, head.Height);
                }
                pic_new.Image = head;
                index++;
            }
            catch (Exception ex)
            {
                MessageBox.Show("出错了："+ ex.Message,"警号",MessageBoxButtons.OK);
            }
        }
       
        /// <summary>
        /// 保存重新绘制的图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_save_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "图片文件|*.png";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                pic_new.Image.Save(saveFileDialog.FileName, ImageFormat.Png);
                MessageBox.Show("保存成功");
            }
        }

    }
}
